// JavaScript Document
var old_n;
var o_n;
var cn = 0;
var btn_status = true;
var get_gamegift = true;
function randomMax(n){
	var s = Math.floor(Math.random()*Math.random()*n);
	if(s>n || s == o_n){
		s= randomMax(n);
	};
	o_n=s;
	return s;
};
function randomNum(){
		var a = Math.random();
		var z;
		if(a>0.001){
			z=Math.floor(a*10);
		}else{
			z = randomNum();
		};
		if(old_n == z){
			z=randomNum();
		}else{
			old_n = z;
		}
		console.log(z);
		return z;
};
var game = function(obj){
	var _this = this;
	this.snow_num = 20;
	this.mask = $('#mask');
	this.geme_content = $('#geme_content');
	this.mask.hide();
	this.start_obj = $('#start');
	this.start_btn = $('#start .origin_btn');
	//this.gray_btn = $('#start .gray_btn');
	this.rule_btn = $('#start .rule_btn');
	this.share = $('#share');

	this.share.bind('click',function(){_this.share.hide();});

	this.no_author = $('#no_author');
	this.no_author.bind('click',function(){
		 window.location.reload();	
	});
	this.game_rule = $('#game_rule');
	this.rule_close = $('#rule_close');

	this.time = obj['time'];
	this.rate = obj['rate'];
	this.min_t = obj['min_t'];

	this.restart = $('#restart');
	this.form = $('#form');
	this.restart_btn = $('#restart_btn');
	this.current_dialog = this.start_obj;
	this.dialog('start');
	this.true_ele='';
	this.math_intval='';

	this.score = 0;
	this.current_level =1;
	this.re_current_level='';

	this.gift_dialog_confirm = $('#gift_dialog_confirm');
	this.rule_pop = $('#rule_pop');
	this.rule_pop.bind('click',function(){
									_this.dialog.call(_this,'rule');
									_this.re_current_level=_this.restart;
									_this.re_current_level.show();
								});

	this.gift_code_back = $('#gift_code_back');
	this.gift_code_back.bind('click',function(){
									_this.mask.show();
									_this.current_dialog.removeClass('down_animation');
									_this.current_dialog.addClass('up_animation');
									_this.current_dialog.bind('webkitAnimationEnd',function(){
										_this.mask.hide();
										_this.current_dialog.removeClass('up_animation');
										_this.current_dialog.unbind('webkitAnimationEnd');
										_this.dialog();
										_this.current_dialog= _this.re_current_level;
										_this.re_current_level='';
									});
								});

	this.snowInter = '';

	this.rule_close.bind('click',function(){
		_this.mask.show();
									_this.current_dialog.removeClass('down_animation');
									_this.current_dialog.addClass('up_animation');
									_this.current_dialog.bind('webkitAnimationEnd',function(){
										_this.current_dialog.removeClass('up_animation');
										_this.current_dialog.unbind('webkitAnimationEnd');
										_this.mask.hide();
										_this.dialog();
										_this.current_dialog= _this.re_current_level;
										_this.re_current_level='';
									});
								});
	/*this.gray_btn.bind('click',function(){
									_this.new_start.call(_this);
								});*/
	this.share_restart_btn = $('#share_restart_btn');
	this.share_restart_btn.bind('click',function(){
									//_this.dialog.call(_this,'start');
									_this.share.hide();
								});
	
	this.contact_btn = $('#contact_btn');
	this.contact_btn.bind('click',function(){
									var status = true;
									var inpt = $('#finput input');
									inpt.each(function(index, element) {
                                        if(!$(element).val()) status = false;
                                    });
									$('.error').hide();
									var param = {name:$(inpt[0]).val(),phone:$(inpt[1]).val(),address:$(inpt[2]).val()};
									if(status){
										$.post(user_url,param,
										  function(result){
											  if(result['status'] == true){
												$('#finput').hide();  
												$('#fstatus').show(); 
											  };
										  });
									}else{
										$('.error').show();
									};
								});
	this.back_game_form = $('#back_game_form');
	this.back_game_form.bind('click',function(){
		_this.mask.show();
									_this.current_dialog.removeClass('down_animation');
									_this.current_dialog.addClass('up_animation');
									_this.current_dialog.bind('webkitAnimationEnd',function(){
										_this.mask.hide();
										_this.current_dialog.removeClass('up_animation');
										_this.current_dialog.unbind('webkitAnimationEnd');
										_this.dialog();
										_this.current_dialog= _this.re_current_level;
										_this.re_current_level='';
									});
								});
								
	this.back_game_form_play = $('#back_game_form_play');
	this.back_game_form_play.bind('click',function(){
		_this.mask.show();
									_this.current_dialog.removeClass('down_animation');
									_this.current_dialog.addClass('up_animation');
									_this.current_dialog.bind('webkitAnimationEnd',function(){
										_this.mask.hide();
										_this.current_dialog.removeClass('up_animation');
										_this.current_dialog.unbind('webkitAnimationEnd');
										_this.dialog();
										_this.current_dialog= _this.re_current_level;
										_this.re_current_level='';
									});
								});
	
	this.rule_btn.bind('click',function(){
									_this.dialog.call(_this,'rule');
									_this.re_current_level=_this.start_obj;
									_this.re_current_level.show();
								});
	this.restart_btn.bind('click',function(){
									_this.new_start.call(_this);
								});
	this.start_btn.bind('click',function(){
									_this.new_start.call(_this);
								});

	this.get_gift_btn = $('#get_gift_btn');
	this.gift_code = $('#gift_code');
	this.get_gift_btn.bind('click',function(){
		//alert(get_gamegift);
									if(get_gamegift == true){
										get_gamegift = false;
										if(_this.current_level<level_rate[0]){
											
										}else 
										if(_this.current_level>=level_rate[0] && _this.current_level<level_rate[1]){
											_this.mask.show();
											_this.gift_dialog_confirm.show();
											$('#gift_text_false').html('继续闯关得288元的礼包?');
											_this.gift_dialog_confirm.css('left',($(document).width()-_this.gift_dialog_confirm.width())/2);
										}else if(_this.current_level>=level_rate[1] && _this.current_level<level_rate[2]){
											_this.mask.show();
											_this.gift_dialog_confirm.show();
											$('#gift_text_false').html('继续闯关得588元的礼包?');
											_this.gift_dialog_confirm.css('left',($(document).width()-_this.gift_dialog_confirm.width())/2);
										}else if(_this.current_level>=level_rate[2]){
											$.post(gift_code_url,
												  {level:_this.current_level},
												  function(result){
													  if(result['status'] == true){
														if(result['data']['type']==0){
															$('.gift_contxt').html('恭喜你获得88元道具礼包');
														}else if(result['data']['type']==1){
															$('.gift_contxt').html('恭喜你获得288元道具礼包');
														}else{
															$('.gift_contxt').html('恭喜你获得588元道具礼包');
														};
														$('#gift_pack_code').html(result['data']['code']);
														_this.gift_dialog_confirm.hide();
														_this.mask.hide();
														_this.dialog.call(_this,'gift_code');
														_this.re_current_level=_this.restart;
														_this.re_current_level.show();
													  };
												  });
										};
								
								
										
									}else{
										$.post(gift_code_url,
										  {level:_this.current_level},
										  function(result){
											  if(result['status'] == true){
												if(result['data']['type']==0){
													$('.gift_contxt').html('恭喜你获得88元道具礼包');
												}else if(result['data']['type']==1){
													$('.gift_contxt').html('恭喜你获得288元道具礼包');
												}else{
													$('.gift_contxt').html('恭喜你获得588元道具礼包');
												};
												$('#gift_pack_code').html(result['data']['code']);
												_this.gift_dialog_confirm.hide();
												_this.mask.hide();
												_this.dialog.call(_this,'gift_code');
												_this.re_current_level=_this.restart;
												_this.re_current_level.show();
											  };
										  });
									};

									/*_this.dialog.call(_this,'gift_code');
									_this.re_current_level=_this.restart;
									_this.re_current_level.show();*/
								});
	this.cancel = $('#sure');
	this.cancel.bind('click',function(){
		_this.gift_dialog_confirm.hide();
		_this.mask.hide();
	});
	this.sure = $('#cancel');
	this.sure.bind('click',function(){
		$.post(gift_code_url,
			  {level:_this.current_level},
			  function(result){
				  if(result['status'] == true){
				    if(result['data']['type']==0){
						$('.gift_contxt').html('恭喜你获得88元道具礼包');
					}else if(result['data']['type']==1){
						$('.gift_contxt').html('恭喜你获得288元道具礼包');
					}else{
						$('.gift_contxt').html('恭喜你获得588元道具礼包');
					};
				    $('#gift_pack_code').html(result['data']['code']);
					_this.gift_dialog_confirm.hide();
					_this.mask.hide();
					_this.dialog.call(_this,'gift_code');
					_this.re_current_level=_this.restart;
					_this.re_current_level.show();
				  };
			  });
	});
};
game.prototype.start_init = function(){
	var _this = this;
	_this.sn_num = 0;
	this.snowInter = window.setInterval(function(){
		if(_this.sn_num>20)return;
		var b = document.createElement('b');
		b.className = 'snow';
		var r = Math.random();
		b.style.left = Math.floor(r*80)+"%";

		b.style.zoom = r;
		b.addEventListener('webkitAnimationEnd',function(event){
			var target = event.target;
			target.parentElement.removeChild(target);
			_this.sn_num=-1;
		});
		_this.start_obj.append(b);
	},1000);

	/*$('#box').bind('webkitAnimationEnd',function(event){
			var target = event.target;
			$(target).removeClass('box_an');
			$(target).addClass('bg_wiggle');
			$(target).unbind('webkitAnimationEnd');
	});
	$('#gift').bind('webkitAnimationEnd',function(event){
			var target = event.target;
			$(target).removeClass('gift_an');
			$(target).addClass('bg_wiggle');
			$(target).unbind('webkitAnimationEnd');
	});*/
};
game.prototype.new_start = function(){
	var _this = this;
	$.post(play_start_url,
			  {},
			  function(result){
				if(result['status']==true){
					if(result['data']['status'] == true){
						//alert('data[status]:'+result['data']['status']);
						get_gamegift = false;
						/*_this.dialog('gift_code');
						if(result['data']['type']==0){
							$('.gift_contxt').html('恭喜你获得88元道具礼包');
						}else if(result['data']['type']==1){
							$('.gift_contxt').html('恭喜你获得288元道具礼包');
						}else{
							$('.gift_contxt').html('恭喜你获得588元道具礼包');
						};
						$('#gift_pack_code').html(result['data']['code']);
						return;*/
					};

					if(result['data']['life']<0){
						_this.share.show();
						return;
					};
						_this.current_dialog.removeClass('down_animation');
						_this.current_dialog.addClass('up_animation');
						_this.restart_game();
						_this.start();
						_this.mask.show();
						_this.current_dialog.bind('webkitAnimationEnd',function(){
							_this.mask.hide();
							_this.current_dialog.removeClass('up_animation');
							_this.current_dialog.unbind('webkitAnimationEnd');
							_this.dialog();
						});

				}else{
							_this.dialog('no_author');
				};
		});

		/*var result={status:true,data:{num:1}}
		if(result['status']==true){
					if(result['data']['num']<=0){
						_this.dialog('share');
						return;
					};

						_this.current_dialog.addClass('up_animation');
						_this.restart_game();
						_this.start();
						_this.current_dialog.bind('webkitAnimationEnd',function(){
							_this.current_dialog.removeClass('up_animation');
							_this.current_dialog.unbind('webkitAnimationEnd');
							_this.dialog();
						});

		}else{
					_this.dialog('no_author');
		};*/
};
game.prototype.dialog = function(dialog_name){
	var _this = this;
	if(this.current_dialog) this.current_dialog.hide();
	this.mask.hide();
	window.clearInterval(this.snowInter);
	switch(dialog_name){
		case 'share':
			this.current_dialog= this.share;
		break;
		case 'no_author':
			this.current_dialog= this.no_author;
		break;
		case 'restart':
			this.current_dialog= this.restart;
		break;
		case 'form':
			this.current_dialog= this.form;
		break;
		case 'rule':
			this.current_dialog= this.game_rule;
		break;
		case 'start':
			this.current_dialog= this.start_obj;
			this.start_init();
		break;
		case 'gift_code':
			this.current_dialog= this.gift_code;
		break;
		default:
			this.current_dialog='';
		break;
	};
	if(this.current_dialog){
		this.current_dialog.show();
		this.mask.show();
		_this.current_dialog.removeClass('down_animation');
		_this.current_dialog.removeClass('up_animation');
		this.current_dialog.addClass('down_animation');
		this.current_dialog.bind('webkitAnimationEnd',function(){
			_this.mask.hide();
							_this.current_dialog.removeClass('down_animation');
							_this.current_dialog.unbind('webkitAnimationEnd');

						});
	};

};
game.prototype.start = function(){

	this.geme_content.css('background',color[cn]);
	cn = cn+1>(color.length-1)?0:cn+1;
	var _this = this;
	var p = photo[randomNum()];
	this.geme_content.html('');
	this.geme_content.append((this.createTable(p)));
	var s = $('#geme_content li div');
	var w = $(s[0]).width();
	s.each(function(index, element) {
        element.style.height = w+'px';
    });

	var n = randomMax(s.length);
	this.true_ele = $(s[n]);
	s[n].firstChild.className = p[0];
	//s[n].firstChild.style.background="#f00";
	s[n].firstChild.onclick = function(){
		_this.nextLevel(true);
	};
	window.clearInterval(_this.math_intval);
	this.time_round();
};
game.prototype.time_round = function(){
	var _this =this;
	var ztime = this.time - (this.rate*(this.current_level-1));
	var math_time = ztime<= this.min_t?this.min_t:ztime;
	$("#m_t").html(math_time/1000);
	this.math_intval = window.setInterval(function(){
		math_time -= 1000;
		if(math_time<=0){
			window.clearInterval(_this.math_intval);

			math_time = 0;
			_this.nextLevel(false);
			//alert('over');
		};
		$('#m_t').html(math_time/1000);

	},1000);
};
game.prototype.ele = function(name,status){
	var _this = this;
	var ele = document.createElement('div');
	var b = document.createElement('b');
	b.style.backgroundColor=color_in[cn];
	ele.appendChild(b);
	b.className = name;
	b.onclick = function(){
		_this.nextLevel(status);
	};
	return ele;
};
game.prototype.nextLevel = function(n){
	if(!btn_status) return;
	btn_status = false;
	var _this = this;
	if(n==true){
		this.current_level++;
		$('#level').html('第'+this.current_level+'关');
		this.geme_content.html('');
		this.start();
		btn_status = true;
	}else{
		$(this.true_ele).addClass('wiggle');
		window.clearInterval(this.math_intval);   // rank 当前排名 top_rank 最高排名 top——level 最高官数 level当前关数 life 剩余次数  code {true 可以填 false:CODe 发完 string 礼品码 null 空}
		_this.mask.show();
		$.post(play_end_url,
			  {level:_this.current_level},
			  function(result){
				_this.true_ele.bind('webkitAnimationEnd',function(){
					_this.mask.hide();
							_this.true_ele.removeClass('wiggle');
							if(result['status']==true){
								//$('#c_pos').html(result['data']['rank']);
								$('.res_level').html(result['data']['top_rank']);
								$('#t_pos').html(_this.current_level);
								_this.dialog('restart');
								btn_status = true;
								
								var life = result['data']['life']==0?1:(result['data']['life']<0?0:result['data']['life']);
								if(_this.current_level<level_rate[0]){
									$('#cong_gift').html('再闯过'+(parseInt(level_rate[0])-_this.current_level)+'关可以获得88礼包<br>剩余游戏次数:'+life);
									$('#get_gift_btn').hide();
								}else if(_this.current_level>=level_rate[0] && _this.current_level<level_rate[1]){
									$('#cong_gift').html('可以获得88元道具礼包<br>再闯过'+(parseInt(level_rate[1])-_this.current_level)+'关可以获得288礼包<br>剩余游戏次数:'+life);
									$('#get_gift_btn').html('领取88元道具礼包');
									$('#get_gift_btn').show();
								}else if(_this.current_level>=level_rate[1] && _this.current_level<level_rate[2]){
									$('#cong_gift').html('可以获得288元道具礼包<br>再闯过'+(parseInt(level_rate[2])-_this.current_level)+'关可以获得588礼包<br>剩余游戏次数:'+life);
									$('#get_gift_btn').html('领取288元道具礼包');
									$('#get_gift_btn').show();
								}else if(_this.current_level>=level_rate[2]){
									$('#cong_gift').html('可以获得588元道具礼包<br>玩的越高获得中奖几率越大<br>剩余游戏次数:'+life);
									$('#get_gift_btn').html('领取588元道具礼包');
									$('#get_gift_btn').show();
								};
								
								if(result['data']['need_info'] == true){
									_this.dialog.call(_this,'form');
									_this.re_current_level=_this.restart;
									_this.re_current_level.show();
								};
							};
						});
		});

		//var result = {status:true,data:{rank:1,top_rank:2,player_num:1}}

		/*this.true_ele.bind('webkitAnimationEnd',function(){

							_this.true_ele.removeClass('wiggle');
							if(result['status']==true){
								//$('#c_pos').html(result['data']['rank']);
								$('#t_pos').html(result['data']['top_rank']);
								_this.dialog('restart');
								btn_status = true;
								if(_this.current_level<level_rate[0]){
									$('#cong_gift').html('再闯过'+(20-_this.current_level)+'关可以获得88礼包<br>剩余游戏次数:'+result['data']['player_num']);
									$('#get_gift_btn').hide();
								}else if(_this.current_level>=level_rate[0] && _this.current_level<level_rate[1]){
									$('#cong_gift').html('可以获得88元道具礼包<br>再闯过'+(30-_this.current_level)+'关可以获得288礼包<br>剩余游戏次数:'+result['data']['player_num']);
									$('#get_gift_btn').html('领取88元道具礼包');
									$('#get_gift_btn').show();
								}else if(_this.current_level>=level_rate[1] && _this.current_level<level_rate[2]){
									$('#cong_gift').html('可以获得288元道具礼包<br>再闯过'+(50-_this.current_level)+'关可以获得588礼包<br>剩余游戏次数:'+result['data']['player_num']);
									$('#get_gift_btn').html('领取288元道具礼包');
									$('#get_gift_btn').show();
								}else if(_this.current_level>=level_rate[2]){
									$('#cong_gift').html('可以获得588元道具礼包<br>玩的越高获得中奖几率越大<br>剩余游戏次数:'+result['data']['player_num']);
									$('#get_gift_btn').html('领取588元道具礼包');
									$('#get_gift_btn').show();
								};

							};
						});*/



	};
};

game.prototype.createTable = function(p){

	var n = this.current_level+1>6?6:this.current_level+1;
	var ul = document.createElement('ul');
	ul.className = 'lin'+n;
	var first = false;
	var t = true;
	var ele;
	for(var r=0;r<n;r++){
		var li = document.createElement('li');
		ul.appendChild(li);
		for(var z=0;z<n;z++){
			/*if(Math.random()<0.05 && t==true){
				first = true;
				t=false;
			};
			if(first){
				ele = this.ele(p[0],first);
				ele.firstChild.style.background = '#fff';
				first = false;
			}else{
				ele = this.ele(p[1],first);
			};
			if(z==(n-1) && t == true && r == (n-1)){
				ele = this.ele(p[0],true);
				ele.firstChild.style.background = '#f00';
			};*/
			ele = this.ele(p[1],false);
			li.appendChild(ele);
		}
	};
	return ul;
};
game.prototype.restart_game = function(){
	this.current_level =1;
	$('#level').html('第'+this.current_level+'关');
	this.score = 0;
	this.true_ele='';
};





function onBridgeReady() {//当用户分享信息时
	WeixinJSBridge.on('menu:share:appmessage', function(argv)
	{
		// alert('appmessage');
		WeixinJSBridge.invoke('sendAppMessage',wxData,function(data){/**这里是我们的重点,分享回调函数,我们通过 回调函数中的参数可以判断用户做了那种操作**/
			if(data.err_msg=="send_app_msg:confirm")
			{

				send_intergal();//当分享成功时，我们调用我们自己定义函数
			}

		});
	});
	WeixinJSBridge.on('menu:share:timeline', function(argv){//当用户分享到朋友圈
	    WeixinJSBridge.invoke('shareTimeline',wxData,function(data){/**这里是我们的重点,分享回调函数,我们通过**/
	    	// alert('shareTimeline' + data.err_msg);
			if(data.err_msg=="share_timeline:ok") {
			 	// alert('hi');
			 	send_intergal();
			 }
		});
	});
}
//这里是我们定义的当分享成功后处理函数，一般是ajax，这里的回调函数不能保证百分百都执行成功。
function send_intergal(){
	$.post(share_url,{},function(result){
		alert('您的分享已成功，增加一次挑战机会');
		//alert(result['data']);
		var sss = $('#cong_gift').html().split(':')[0];
		$('#cong_gift').html(sss+':'+result['data']);
	});
}
//当用户进行页面加载时,将Weixin浏览器定义的事件进行绑定。
if (typeof WeixinJSBridge === "undefined"){
	if (document.addEventListener){
		document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
	}
}else{
	onBridgeReady();
}

